﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BSCore
{
    public class Camera2D
    {
        protected float _zoom= 1.0f; // Camera Zoom
        public Matrix _transform; // Matrix Transform
        public Vector2 position = Vector2.Zero; // Camera Position
        protected float _rotation = 0f; // Camera Rotation
        private Vector2 mapSize;

        public Vector2 Size { get; set; }


        // Sets and gets zoom
        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; } // Negative zoom will flip image
        }

        public float Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }

        // Auxiliary function to move the camera
        public void Move(Vector2 amount)
        {
            position += amount;
        }
        // Get set position
        public Vector2 Position
        {
            get { return position; }
            set 
            { 
                position = new Vector2(
                    MathHelper.Clamp(
                        value.X,
                        0f,
                        mapSize.X-Size.X),
                    MathHelper.Clamp(
                        value.Y,
                        0f,
                        mapSize.Y-Size.Y )); 
            }
        }


        public Matrix get_transformation(GraphicsDevice graphicsDevice)
        {
            _transform =       // Thanks to o KB o for this solution
              Matrix.CreateTranslation(new Vector3(-position.X, -position.Y, 0)) *
                                         Matrix.CreateRotationZ(Rotation) *
                                         Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                                         Matrix.CreateTranslation(new Vector3(Size.X * 0.5f, Size.Y * 0.5f, 0));
            return _transform;
        }

    }
}
